<?php

namespace App;

use App\model\WechatUser;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Medz\Laravel\Notifications\JPush\Sender;

/**
 * App\User
 *
 * @property int $id 主键
 * @property string $username 管理后台账号
 * @property string $password 管理后台密码
 * @property string $salt 安全符
 * @property string $img 头像
 * @property string $thumb_img 头像缩略图
 * @property int $create_time
 * @property string $realname 真实姓名
 * @property string $num 员工编号
 * @property string $email 邮箱
 * @property string $mobile 手机号码
 * @property string $sex 男、女
 * @property int $structure_id 部门
 * @property string $post 岗位
 * @property int $status 状态,0禁用,1启用,2未激活
 * @property int $parent_id 直属上级ID
 * @property string $authkey 验证信息
 * @property int $authkey_time 验证失效时间
 * @property int $type 1系统用户 0非系统用户
 * @property-read \Illuminate\Notifications\DatabaseNotificationCollection|\Illuminate\Notifications\DatabaseNotification[] $notifications
 * @property-read int|null $notifications_count
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User query()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereAuthkey($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereAuthkeyTime($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereCreateTime($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereEmail($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereImg($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereMobile($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereNum($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereParentId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User wherePassword($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User wherePost($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereRealname($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereSalt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereSex($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereStatus($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereStructureId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereThumbImg($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereType($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereUsername($value)
 * @mixin \Eloquent
 * @property int|null $can_genjin 是否能跟进
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereCanGenjin($value)
 * @property-read \App\model\WechatUser|null $wechatUser
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Project[] $project
 * @property-read int|null $project_count
 * @property int|null $is_new
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereIsNew($value)
 * @property int|null $is_display  免打搅
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereIsDisplay($value)
 */
class User extends Authenticatable
{
    use Notifiable;
    protected $table="5kcrm_admin_user";
    public $timestamps=null;
    protected function routeNotificationForJpush()
    {
        return new Sender([
            'platform' => 'all',
            'audience' => [
                'alias' => sprintf('%d', $this->id),
            ],
        ]);
    }
    public function wechatUser(){
       return $this->hasOne(WechatUser::class,'admin_user_id','id');
    }
    public function  project(){
        return $this->belongsToMany(Project::class,'5kcrm_admin_user_project','admin_user_id','project_id','id','project_id');
    }
    public static function getSelectOptions($project_id='')
    {
        $options = self::select('id','realname as text');
        if ($project_id){
          $options=$options->whereHas('project',function ($q)use ($project_id){
              $q->where('5kcrm_project.project_id',$project_id);
          });
        }
        $options=$options->get();
        $selectOption = [];
        foreach ($options as $option){
            $selectOption[$option->id] = $option->text;
        }
        return $selectOption;
    }
}
